#include <jni.h>
#include <string>
#include <spline.h>
#include <android/log.h>

#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,"DrawView",__VA_ARGS__)

std::vector<double> T;
std::vector<double> X;
std::vector<double> Y;
float interval = 0;


extern "C"
JNIEXPORT void JNICALL
Java_com_bytecodezz_app_spline_NativeController_nativeDown(JNIEnv *env, jobject thiz, jfloat x,
                                                           jfloat y) {
    T = std::vector<double>();
    X = std::vector<double>();
    Y = std::vector<double>();
    X.push_back(x);
    Y.push_back(y);
}

extern "C"
JNIEXPORT jfloatArray JNICALL
Java_com_bytecodezz_app_spline_NativeController_nativeMove(JNIEnv *env, jobject thiz, jfloat x,
                                                           jfloat y) {

    jfloatArray result;
    //三次样条函数计算
    if (X[X.size() - 1] != x && Y[Y.size() - 1] != y) {
        //与前一个点不相同才添加成样本，因为参数样条也不能出现相同的点

        float distEnd = sqrt(
                (x - X[X.size() - 1]) * (x - X[X.size() - 1]) +
                (y - Y[Y.size() - 1]) * (y - Y[Y.size() - 1]));
        if (distEnd >= interval) {
            X.push_back(x);
            Y.push_back(y);

        }
    }
    std::vector<float> dots = std::vector<float>();
    if (X.size() >= 3) {
        //根据已有样本计算出T距离
        T.resize(0);
        T.push_back(0.0);
        for (int i = 1; i < X.size(); ++i) {
            float dist = T[i - 1] + sqrt(
                    (X[i] - X[i - 1]) * (X[i] - X[i - 1]) + (Y[i] - Y[i - 1]) * (Y[i] - Y[i - 1]));
            T.push_back(dist);
        }
        LOGE("C++ spline dot = %d", X.size());
        tk::spline splineX(T, X), splineY(T, Y);
        //从样条曲线中取出所有点
        //整个曲线长度
        double length = T.at(T.size() - 1);
        float step = 2.5; //一个点的长度
        int size = floor(length / step); //点的个数
        float t = 0.0;
        while (t <= length) {
            dots.push_back(splineX(t));
            dots.push_back(splineY(t));
            t += step;
        }
    } else {
        //从普通直线中取出所有点
//        float length = 0.0;
//        for (int i = 1; i < X.size(); ++i) {
//            length += sqrt(
//                    (X[i] - X[i - 1]) * (X[i] - X[i - 1]) + (Y[i] - Y[i - 1]) * (Y[i] - Y[i - 1]));
//        }
        // 组装返回
        for (int i = 0; i < X.size(); ++i) {
            dots.push_back(X[i]);
            dots.push_back(Y[i]);
        }
    }

    // 组装返回
    result = (env)->NewFloatArray(dots.size());
    float *buffer = new float[sizeof(float) * dots.size()];
    if (!dots.empty()) {
        memcpy(buffer, &dots[0], dots.size() * sizeof(float));
    }
    env->SetFloatArrayRegion(result, 0, dots.size(), buffer);
    return result;
}

extern "C"
JNIEXPORT void JNICALL
Java_com_bytecodezz_app_spline_NativeController_nativeUp(JNIEnv *env, jobject thiz, jfloat x,
                                                         jfloat y) {

}extern "C"
JNIEXPORT void JNICALL
Java_com_bytecodezz_app_spline_NativeController_nativeSetSmooth(JNIEnv *env, jobject thiz,
                                                                jfloat size) {
    interval = size;
}